<?php
namespace App\Http\Controllers;

use DB;
use Log;
use Session;
use Request;

class MessageController extends Controller
{
    /**
     * 用户登录
     * @Author   tianyu
     * @DateTime 2017-04-06T17:38:29+0800
     * @param    \Illuminate\Http\Request
     */
    public function login(\App\Http\Requests\LoginPostRequest $request)
    {
        $input = Request::all();
        $user = DB::table('user')->where('user_name', $input['user_name'])->first();
        if ($user) {
            if (password_verify($input['password'], $user->password) && $user->user_name == $input['user_name']) {
                Log::info("username:{$user->user_name} login success~");
                //登录成功，存储session
                Session::put('uid', $user->id);
                //Session::save();  //使用redis存储时不需要save();
                return redirect('message/content');
            } else {
                return "用户名或密码错误！";
            }
        } else {
            return "用户名不存在！";
        }
    }

    /**
     * 用户退出登录
     */
    public function quit()
    {
        Session::forget('uid');
        return redirect('message/content');
    }

    /**
     * 用户注册
     * @param  \App\Http\Requests\RegistPostRequest
     */
    public function regist(\App\Http\Requests\RegistPostRequest $request)
    {
        $input = request::all();
        //注册
        $user = new \App\User;
        $user->user_name = $input['user_name'];
        $user->email     = filter_var($input['email'], FILTER_SANITIZE_EMAIL);
        $user->password  = bcrypt($input['password']);
        $user->create_time  = config('constants.CURRENT_TIME');
        $user->status  = 1;

        if ($user->save()) {
            Log::info("username:{$user->user_name} regist success~");
        }

        return view('messages.login');
    }

    /**
     * 管理员页面
     */
    public function manage()
    {
        if ($this->is_login && $this->is_admin) {
            $users = DB::table('user')->orderBy('create_time','asc')->paginate(10);
            return view('messages.manage', ['users' => $users]);
        } else {
            return "请用管理员身份登录";
        }
    }

    /**
     * 设置是否拉入黑名单
     */
    public function handle_member()
    {
        //判断是否登录且是管理员
        if ($this->is_login && $this->is_admin) {
            $uid = request::get('uid');
            $res = DB::update('update user set status=status^1 where id=?', [$uid]);
            if ($res) {
                Log::info("uid:{$this->uid} update {$uid} is_black success~");
            } else {
                Log::info("uid:{$this->uid} update {$uid} is_black fail~");
            }
            return redirect('message/manage');
        } else {
            return "请用管理员身份登录";
        }
    }

    /**
     * 设置是否为管理员
     */
    public function handle_admin()
    {
        //判断是否登录且是管理员
        if ($this->is_login && $this->is_admin) {
            $uid = request::get('uid');
            $res = DB::update('update user set is_admin=is_admin^1 where id=?', [$uid]);
            if ($res) {
                Log::info("uid:{$this->uid} update {$uid} is_admin success~");
            } else {
                Log::info("uid:{$this->uid} update {$uid} is_admin fail~");
            }

            return redirect('message/manage');
        } else {
            return "请用管理员身份登录";
        }
    }

    /**
     * 留言板页面
     */
    public function content()
    {
        $user_status = 0;
        //判断是否登录
        if ($this->is_login) {
            $user_status = 1;
            $user = parent::get_user($this->uid);
            //判断用户身份
            if (empty($user->status)) {
                $user_status = 2;
            }

            //判断用户是否在黑名单
            if (!$this->is_black) {
                if ($message = Request::get('message')) {
                    $res = DB::table('comment')->insert([
                        'uid' => $this->uid,
                        'text' => $message,
                        'create_time' => config('constants.CURRENT_TIME')
                    ]);

                    if (!$res) {
                        Log::info("uid:{$this->uid} comment success~");
                    }

                    return redirect('message/content');
                }
            }
        }

        //获取留言
        $comments = parent::get_comments();
        //留言时间转换
        foreach ($comments as $value) {
            $left_time = config('constants.CURRENT_TIME') - $value->create_time;
            if ($left_time < 60*60) {
                $value->create_time =  floor($left_time/60).'分钟前';
            } elseif($left_time < 60*60*24) {
                $value->create_time =  floor($left_time/60/60).'小时前';
            } elseif($left_time < 60*60*24*30) {
                $value->create_time =  floor($left_time/60/60/24).'天前';
            } else {
                $value->create_time =  date('Y-m-d H:i:s', $value->create_time);
            }
        }

        return view('messages.content', [
            'comments' => $comments,
            'user_status' => $user_status,
            'is_admin' => $this->is_admin
        ]);
    }
}

?>